package com.techsen.tsweb.sys.security;

import java.util.ArrayList;
import java.util.List;

import com.techsen.tsweb.core.domain.TreeNode;
import com.techsen.tsweb.core.util.ValidUtil;
import com.techsen.tsweb.sys.domain.Menu;
import com.techsen.tsweb.sys.domain.Oper;

public class ResourceNode extends TreeNode<ResourceNode> {

    private static final long serialVersionUID = -1987230579664342753L;
    
    /**
     * 资源名
     */
    private String name;
    
    /**
     * 资源类型
     */
    private String resourceType;
    
    /**
     * 是否选中
     */
    private boolean checked;
    
    public String getName() {
        return name;
    }

    public ResourceNode setName(String name) {
        this.name = name;
        return this;
    }

    public String getResourceType() {
        return resourceType;
    }

    public ResourceNode setResourceType(String resourceType) {
        this.resourceType = resourceType;
        return this;
    }
    
    public boolean isChecked() {
        return checked;
    }

    public ResourceNode setChecked(boolean checked) {
        this.checked = checked;
        return this;
    }

    /**
     * 将Resource转换为ResourceNode
     */
    public static ResourceNode convert(Resource resource) {
        ResourceNode node = null;
        if (resource != null) {
            node = new ResourceNode();
            if (Menu.class.getSimpleName().toLowerCase().equals(resource.getResourceType())) {
                Menu menu = (Menu) resource;
                node.setId(menu.getId())
                    .setName(menu.getName())
                    .setParentId(menu.getParentId())
                    .setResourceType(menu.getResourceType());
            } else if (Oper.class.getSimpleName().toLowerCase().equals(resource.getResourceType())) {
                Oper oper = (Oper) resource;
                node.setId(oper.getId())
                    .setName(oper.getName())
                    .setParentId(oper.getMenuId())
                    .setResourceType(oper.getResourceType());
            }
        }
        return node;
    }
    
    /**
     * 将Resource集合转换为ResourceNode集合
     */
    public static List<ResourceNode> convert(List<Resource> resources) {
        List<ResourceNode> list = new ArrayList<ResourceNode>();
        if (ValidUtil.isValid(resources)) {
            for (Resource resource : resources) {
                list.add(convert(resource));
            }
        }
        return list;
    }
    
}
